<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class News extends MX_Controller {

     var $_table = 'news';

     function __construct() {
		parent::__construct ();
		$this->load->model( 'news_model' );

		$_SESSION ['KCFINDER'] = array ();
		$_SESSION ['KCFINDER'] ['disabled'] = false;

		$this->load->library ( 'ckeditor', array ('instanceName' => 'CKEDITOR1', 'basePath' => base_url () . "ckeditor/", 'outPut' => true ) );
	}

	function listnews() {
	$data ['list'] = $this->news_model->getnew();
	$data ['content'] = 'admin/list';
	$this->load->view ( 'back_end/index', $data );
	}

	function form_add_news() {
	$data ['content'] = 'admin/form_add_news';
	$this->load->view ( 'back_end/index', $data );
	}

	function do_add_news() {

	  if ($this->input->post ()) {
			$data = array ();
			$data ['ten_news'] = ( string ) $this->input->post ( 'ten_news' );
			$data ['ten_news_en'] = ( string ) $this->input->post ( 'ten_news_en' );
			$data ['news_content'] = ( string ) $this->input->post ( 'news_content' );
			$data ['news_content_en'] = ( string ) $this->input->post ( 'news_content_en' );
			$data ['createdtime'] = date('Y-m-d H:i:s');
			$this->news_model->saveItem ( 'news', array ('field' => 'id', 'id' => 0 ), $data );
			redirect ( 'admin/news/listnews' );
		}

	}

	function form_sua_new(){

	   $id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		/*
		if ($this->input->post ()) {
			$_id = $this->input->post ( 'id' );
			$data = array ();
			$data ['ten_news'] = ( string ) $this->input->post ( 'ten_news' );
			$data ['news_content'] = ( string ) $this->input->post ( 'news_content' );
			$data ['news_content'] = ( string ) $this->input->post ( 'news_content' );
			$this->news_model->saveItem ( 'news', array ('field' => 'id', 'id' => $_id ), $data );
			redirect ( 'admin/views/listnews' );
		}
		*/
		$data ['detail'] = $this->news_model->getDetail( $id );
		$data ['content'] = 'admin/form_sua_new';
		$this->load->view ( 'back_end/index', $data );

	}

	function do_sua_new() {
	   if ($this->input->post ()) {
			$id = $this->input->post ( 'id_de_sua' );
			$data ['ten_news'] = ( string ) $this->input->post ( 'ten_news' );
			$data ['ten_news_en'] = ( string ) $this->input->post ( 'ten_news_en' );
			$data ['news_content'] = ( string ) $this->input->post ( 'news_content' );
			$data ['news_content_en'] = ( string ) $this->input->post ( 'news_content_en' );
			$this->news_model->save_new_sua ($data,$id );
			redirect ( 'admin/news/listnews' );
		}

	}

	function xoanew($id = NULL) {
		if ($id == NULL) {
			$id = ( int ) $this->uri->segment ( 4 );
		}
		$this->news_model->del ( $id );
		redirect ( 'admin/news/listnews' );
	}

}